package cn.xu.bookstore.book.web.servlet.admin;



import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;
import cn.xu.bookstore.book.domain.Book;
import cn.xu.bookstore.book.service.BookService;
import cn.xu.bookstore.category.domain.Category;
import cn.xu.bookstore.category.service.CategoryService;

public class AdminBookServlet extends BaseServlet {
	private BookService bookService  = new BookService();
	private CategoryService categoryService = new CategoryService();
	public String findAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Book> books = bookService.findAll();
		request.setAttribute("books", books);
		return "f:/adminjsps/admin/book/list.jsp";
	}
	
	public String load(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 查处该书的详情
		 * 和该书的 分类
		 */
		String bid = request.getParameter("bid");
		Book book  = bookService.load(bid);
		List<Category> categorys = categoryService.findAll();
		
		request.setAttribute("book", book);
		request.setAttribute("categorys", categorys);
		
		return "f:/adminjsps/admin/book/desc.jsp";
	}
	
	public String edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Book book = CommonUtils.toBean(request.getParameterMap(), Book.class);
		
		String cid = request.getParameter("cid");
		Category category = new Category();
		category.setCid(cid);
		book.setCategory(category);
		
		bookService.edit(book);
		return findAll(request,response);
		
	}
	
	public String delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String bid = request.getParameter("bid");
		bookService.delete(bid);
		
		return findAll(request,response);
	}
	
	public String addPre(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Category> categorys = categoryService.findAll();
		request.setAttribute("categorys", categorys);
		return "f:/adminjsps/admin/book/add.jsp";
	}
	
	

}
